<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
cfc('core')->checklogin('jy_kefu/mychat/');
if($_RQ['op']=='home'){
	$setmid=$_SESSION['uid'];
	$chatid=intval($_RQ['chatid']);
	$getmid=intval($_RQ['getmid']);
	if($getmid){
		$chatid=$this->model('kefu')->getchatid($setmid,$getmid);
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'meisdel'=>0,'mid'=>$setmid);
	$fields=array('id','lasttime','chatid','mid','lastmsg','getmid','msgtype');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 60;
	$total= 0;
	$chats = pdo_getslice('jy_kefu_member_chat',$params,array($pindex,$psize),$total,$fields,'','lasttime DESC');
	if($chats){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		foreach($chats as &$item){
			$item['noreadnum']=pdo_count('jy_kefu_chatlog',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$item['chatid'],'getmid'=>$_SESSION['uid'],'isread'=>0));
			if($item['lasttime']>$daytime){
				$seth=date('h',$item['lasttime']);
				$nowh=date('h',time());
				$hour=$nowh-$seth;
				if($hour<1){
					$fens=intval((time-$item['lasttime'])/60);
					//$item['lasttime']=$fens.'分前';
				}else{
					//$item['lasttime']=$hour.'小时前';
				}
				$item['lasttime']=date('m-d',$item['lasttime']);
			}else{
				$item['lasttime']=date('m-d',$item['lasttime']);
			}
			$item['linkurl']=custom_url('jy_kefu/chat//getmid='.$item['getmid'],true);
			$kefu=pdo_get('jy_kefus',array('uniacid'=>$_SESSION['uniacid'],'number'=>$item['getmid']),array('id','name','picurl','isonline'));
			if($item['msgtype'] == 3){
				$item['lastmsg'] = '语音信息...';
			}
			if($item['msgtype'] == 4){
				$item['lastmsg'] = '图片信息...';
			}
			if($kefu){
				$item['title'] = $kefu['name'];
				$item['logo'] = $kefu['picurl'];
			}else{
				$member=pdo_get('jy_kefu_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['getmid']),array('id','name','logo','iskefu'));
				if($member){
					$item['title'] = $member['name'];
					$item['logo'] = $member['logo'];
				}else{
					$member=pdo_get('core_members',array('id'=>$item['getmid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
					$item['title'] = $member['nickname'];
					$item['logo'] = $member['avatar'];
				}
			}
			
		}
	}
	$return['iskefu']=pdo_count('jy_kefus',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$kefu=pdo_get('jy_kefus',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($return['iskefu']){
		$member=$kefu;
		$member['logo']=$member['picurl'];
	}else{
		$member=pdo_get('core_members',array('id'=>$getmid,'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
		if(empty($member)){
			//微信环境
			$member=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'type'=>2,'wxgid'=>$_SESSION['wechat_id'],'openid'=>$_SESSION['openid']),array('id','avatar','nickname'));
		}
		$member['logo']=$member['avatar'];
		$member['name']=$member['nickname'];
	}
	$return['member']=$member;
	$return['noreadnum']=pdo_count('jy_kefu_chatlog',array('uniacid'=>$_SESSION['uniacid'],'getmid'=>$_SESSION['uid'],'isread'=>0));
	$return['list']=$chats;
	$return['chatinfo']['chatid']=$chatid;
	$return['mychaturl']=custom_url('mychat/',true);
	set_html('title','我的对话');
	exi($return);
}
if($_RQ['op']=='del'){
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	if($id){
		pdo_update('jy_kefu_member_chat',array('meisdel'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('删除成功！');
}
if($_RQ['op']=='setstatus'){
	$postdata=get_postdata();
	$status = intval($postdata['status']);
	if($status){
		pdo_update('jy_kefus',array('status'=>$status),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('设置成功');
}